--雷霆反击
local leitingfanji = fk.CreateSkill {
    name = "mcdh_leitingfanji",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

leitingfanji:addEffect('active',{
    prompt = "#mcdh_leitingfanji-active",
    can_use = function(self, player)
        if player:usedSkillTimes(self.name) >= 1 then return false end
        return not player:isKongcheng()
    end,
    card_num = 0,
    card_filter = Util.FalseFunc,
    target_num = 1,
    target_filter = function(self, player,to_select, selected)
        return #selected == 0 and to_select ~= player
                and not to_select:isKongcheng()
    end,
    on_use = function(self, room, effect)
        local player =effect.from
        local target = effect.tos[1]
        local pcids, psuits, pcount = player:getCardIds(Player.Hand), {}, 0
        player:showCards(pcids)
        local tcids, tsuits, tcount = target:getCardIds(Player.Hand), {}, 0
        target:showCards(tcids)
        for _, cid in ipairs(pcids) do
            local card = Fk:getCardById(cid)
            local suit = card:getSuitString(true)
            if not psuits[suit] then
                psuits[suit] = {}
                pcount = pcount + 1
            end
            table.insertIfNeed(psuits[suit], cid)
        end
        for _, cid in ipairs(tcids) do
            local card = Fk:getCardById(cid)
            local suit = card:getSuitString(true)
            if not tsuits[suit] then
                tsuits[suit] = {}
                tcount = tcount + 1
            end
            table.insertIfNeed(tsuits[suit], cid)
        end
        local choices = {}
        if pcount > tcount then
            if tcount > 0 then
                for suit, _ in pairs(psuits) do
                    table.insert(choices, suit)
                end
                local chosen = room:askForChoices(
                        player,
                        choices,
                        tcount, tcount,
                        self.name,
                        "#mcdh_leitingfanji-choice",
                        false
                )
                for _, suit in ipairs(chosen) do
                    for _, cid in ipairs(psuits[suit]) do
                        table.removeOne(pcids, cid)
                    end
                end
            end
            room:throwCard(pcids, self.name, player, player)
            if tcount == 1 then
                local card = Fk:cloneCard("thunder__slash")
                card.skillName = self.name
                for i = 1, #pcids do  
                    if #card:getDefaultTarget(player,{bypass_times=true, extraUse = true}) ~= 0 and not player.dead then
                        room:askToUseVirtualCard(player,{name="thunder__slash",skill_name=self.name,extra_data={bypass_times=true, extraUse = true},cancelable=false})
                    else
                            break
                    end
                end
            end
        else
            if pcount > 0 then
                for suit, _ in pairs(tsuits) do
                    table.insert(choices, suit)
                end
                local chosen = room:askForChoices(
                        target,
                        choices,
                        pcount, pcount,
                        self.name,
                        "#mcdh_leitingfanji-choice",
                        false
                )
                for _, suit in ipairs(chosen) do
                    for _, cid in ipairs(tsuits[suit]) do
                        table.removeOne(tcids, cid)
                    end
                end
            end
            room:throwCard(tcids, self.name, target, target)
        end
    end,
})

Fk:loadTranslationTable {
    ["mcdh_leitingfanji"] = "斥霆",
    [":mcdh_leitingfanji"] = "『雷霆反击』<br>出牌阶段限一次，你可以与一名其他角色展示所有手牌，展示牌的花色数较多的一方将手牌中的花色数弃置至与手牌较少的一方相同。若你因此将手牌中的花色弃置至一种，你每因此弃置一张牌，便可视为使用一张雷【杀】。",
    ["#mcdh_leitingfanji-active"] = "雷霆反击:选择一名其他角色，你将与其展示手牌",
    ["#mcdh_leitingfanji-choice"] = "雷霆反击:选择你要保留的花色牌",
    ["#mcdh_leitingfanji-slash"] = "雷霆反击:选择雷【杀】的目标（还剩 %arg 张【杀】）",
}

return leitingfanji